package com.ruoyi.wb.mapper;

import com.ruoyi.wb.domain.WbPlan;
import com.ruoyi.wb.dto.WbPlanAuditDto;
import com.ruoyi.wb.vo.WbContractVo;
import com.ruoyi.wb.vo.WbPlanAuditLogVo;
import com.ruoyi.wb.vo.WbPlanVo;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

/**
 * 维保计划Mapper接口
 * 
 * @author ruoyi
 * @date 2024-10-14
 */
public interface WbPlanMapper 
{
    /**
     * 查询维保计划
     * 
     * @param id 维保计划主键
     * @return 维保计划
     */
    public WbPlan selectWbPlanById(Long id);

    /**
     * 查询维保计划列表
     * 
     * @param wbPlan 维保计划
     * @return 维保计划集合
     */
    public List<WbPlan> selectWbPlanList(WbPlan wbPlan);

    /**
     * 新增维保计划
     * 
     * @param wbPlan 维保计划
     * @return 结果
     */
    public int insertWbPlan(WbPlan wbPlan);

    /**
     * 修改维保计划
     * 
     * @param wbPlan 维保计划
     * @return 结果
     */
    public int updateWbPlan(WbPlan wbPlan);

    /**
     * 删除维保计划
     * 
     * @param id 维保计划主键
     * @return 结果
     */
    public int deleteWbPlanById(Long id);

    /**
     * 批量删除维保计划
     * 
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
    public int deleteWbPlanByIds(Long[] ids);

    /**
     * 上传任务表单信息查询
     * @param planId
     * @return
     */
    WbContractVo getJobInfoByPlanId(Long planId);

    /**
     * 检查
     *
     * @param userId
     * @return
     */
    List<WbPlan> examineJob(@Param("userId") Long userId, @Param("key") String key,@Param("type") String type);

    @Select("select count(id) from wb_plan where operater_id =#{userId} and date_format(plan_start_date,'%Y-%m')=date_format(now(),'%Y-%m')")
    int getMonthCount(Long userId);

    @Select("select count(id) from wb_plan where operater_id =#{userId} and date_format(plan_start_date,'%Y-%m')=date_format(now(),'%Y-%m') and exec_status = 6")
    int getMonthAccomplish(Long userId);

    @Select("select count(id) from wb_plan where operater_id =#{userId} ")//and date_format(plan_start_date,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')
    int getTodayMaintenanceJob(Long userId);

    @Select("select count(id) from wb_repair_order where repairer_id = #{userId} and business_id is not null ")//and date_format(arrive_time,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d')
    int getTodayMaintainJob(Long userId);

    /**
     * 新增日志记录
     * @param dto
     * @return
     */
    int savePlanAuditLog(@Param("dto") WbPlanAuditDto dto);

    @Select("select count(id)  from wb_plan_audit_log where plan_id = #{planId} and audit_id = #{userId} and  is_delete = 0 ")
    int getPlanAuditLogByPlanId(@Param("planId") Long planId, @Param("userId") Long userId);

    /**
     * 逻辑删除掉之前的审核记录
     * @param planId
     * @return
     */
    @Update("update wb_plan_audit_log set is_delete = 1 where plan_id = #{planId} and is_delete = 0 ")
    void deletePlanAuditLog(@Param("planId") Long planId);

    void todayJob(@Param("planIds") List<Long> planIds);

    /**
     * 今日维保
     * @param userId
     * @return
     */
    List<WbPlan> todayPlan(@Param("execStatus")Integer execStatus,@Param("userId") Long userId);

    /**
     * 获取维保计划审核记录
     * @param planId
     * @return
     */
    List<WbPlanAuditLogVo> getPlanAuditLog(Long planId);

    /**
     * 获取企业维保计划
     * @param contractList
     * @return
     */
    List<WbPlan> getEnterprisePlan(@Param("contractList") List<Long> contractList,@Param("wbPlan")WbPlan wbPlan);

    /**
     * 统计维保审核数据
     * @param userId
     * @return
     */
    WbPlanVo statisticsMaintenanceAuditNumber(@Param("userId")Long userId);

    /**
     * 查询当前登录用户审核信息
     * @param execStatus
     * @param userId
     * @return
     */
    List<WbPlan> selectUserAuditPage(@Param("execStatus")Integer execStatus,@Param("userId") Long userId);


    List<WbPlan> selectWbPlanListByRole(WbPlan wbPlan);

    void updateFireFightingIdByUserId(@Param("userIds") List<Long> userIds,@Param("maintainCompanyId") Long maintainCompanyId,@Param("execStatus")String execStatus);

    void updateEngineerIdByUserId(@Param("userIds") List<Long> userIds,@Param("maintainCompanyId") Long maintainCompanyId,@Param("execStatus")String execStatus);

    void updateOperaterIdUserId(@Param("userIds") List<Long> userIds,@Param("maintainCompanyId") Long maintainCompanyId,@Param("execStatus")String execStatus);

    @Options(useGeneratedKeys = true, keyProperty = "id")
    int batchSave(@Param("wbPlans") List<WbPlan> wbPlans);

    int selectUserAuditPageCount(@Param("userId") Long userId);
}
